package com.uplift.mgt.customer.service.impl;

import java.math.BigDecimal;
import java.util.*;

import com.uplift.common.base.BusinessException;
import com.uplift.common.utils.JExcelReadUtils;
import com.uplift.mgt.building.model.RealtyEarnings;
import com.uplift.mgt.building.model.RealtyInfo;
import com.uplift.mgt.customer.model.Customer;
import com.uplift.mgt.customer.service.ICustomerService;
import com.uplift.sec.SecurityContext;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.uplift.common.utils.UUIDGenerater;
import com.uplift.mgt.customer.dao.IReceiptsMapper;
import com.uplift.mgt.customer.model.Receipts;
import com.uplift.mgt.customer.service.IReceiptsService;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

@Service
@Transactional
public class ReceiptsServiceImpl implements IReceiptsService {

	@Autowired
	private IReceiptsMapper receiptsMapper;

	@Autowired
	private ICustomerService customerService;

	/** excle 每列的字段名称 */
	private String[] excel_cloum_names = new String[] {"客户名称","金额","支付时间","汇款状态","收益开始","收益开始"};

	@Override
	public int saveReceipts(Receipts receipts) {
		if (StringUtils.isBlank(receipts.getId())) {
			if (StringUtils.isBlank(receipts.getCustomerId())) {
				return 2;
			}
			receipts.setId(UUIDGenerater.generate());
			this.receiptsMapper.insertReceipts(receipts);
		} else {
			this.receiptsMapper.updateReceipts(receipts);
		}
		return 1;
	}

	@Override
	public int removeReceipts(Receipts receipts) {
		return this.receiptsMapper.deleteReceipts(receipts);
	}

	@Override
	public int modifyReceipts(Receipts receipts) {
		return this.receiptsMapper.updateReceipts(receipts);
	}

	@Override
	public List<Map<String, Object>> queryReceipts(Receipts receipts) {
		PageHelper.startPage(receipts.getPage(), receipts.getLimit());
		return this.receiptsMapper.selectReceipts(receipts);
	}

	@Override
	public void importReceipts(MultipartHttpServletRequest request) {
		try {
			MultipartFile file = request.getFile("file");
			if(file == null) {
				throw new BusinessException("导入失败，导入文件为空", 400, null);
			}
			List<Map<String, String>> datas = JExcelReadUtils.readExcelSheet1(file.getInputStream(), -1, 2, -1);
			if(CollectionUtils.isEmpty(datas)) {
				throw new BusinessException("导入失败，内容为空", 400, null);
			}

			if(!JExcelReadUtils.checkImprtExcelFormat(datas.get(0), excel_cloum_names)) {
				throw new BusinessException("导入失败，excle 模版错误", 400, null);
			}

			List<Receipts> receipts = createReceipts(datas);
			if(CollectionUtils.isEmpty(receipts)) {
				throw new BusinessException("导入失败，内容为空", 400, null);
			}

			for (Receipts _receipts : receipts) {
				saveReceipts(_receipts);
			}
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw new BusinessException(e);
			} else {
				throw new BusinessException("导入失败", 400, null);
			}
		}
	}

	private List<Receipts> createReceipts(List<Map<String, String>> datas) {

		List<Receipts> receipts = new ArrayList<>();

		Date now = new Date();

		for (int i = 1; i < datas.size(); i++) {
			receipts.add(createReceipts(datas.get(i), i, now));
		}

		return receipts;
	}
	//znl 加入修改  对一下
	private Receipts createReceipts(Map<String, String> data, int row, Date date) {
		String customerName = data.get("0");
		if(StringUtils.isBlank(customerName)) {
			throw new BusinessException(String.format("第[%s]条导入失败，客户名称不能为空", row), 400, null);
		}

		Map<String, Object> customerMap = customerService.queryCustomerByName(customerName);
		if(customerMap == null) {
			throw new BusinessException(String.format("第[%s]条导入失败，客户名称[%s] 不存在",row, data.get("0")), 400, null);
		}

		Receipts receipts = new Receipts();
		receipts.setMoney(data.get("1"));
		receipts.setCustomerId(customerMap.get("id").toString());
		receipts.setCustomerName(customerMap.get("name").toString());
		receipts.setPaymentTime(data.get("2")+ " 00:00:00");
		receipts.setStatus(StringUtils.equals(data.get("3"), "已汇款")?"1":"2");
		receipts.setProfitStart(data.get("4"));
		receipts.setProfitEnd(data.get("5"));

		return receipts;
	}

}
